"""
@author: 周末
--------------------------------------
@project: api-testing-framework
--------------------------------------
@contact:15892766524@163.com
--------------------------------------
@file: Request.py
--------------------------------------
@time: 2021/11/12 9:34
--------------------------------------
"""
import requests


class Request(object):
    @classmethod
    def send(cls, url: str, method: str, params: dict = None, data: dict = None, json=None,
             header: dict = None):
        """
        封装request请求
        :param url: 请求地址
        :param method: 请求方式
        :param params: get 接收参数
        :param data: post x-www-form-urlencode 方式接收参数
        :param json: post json 方式接收参数
        :param header: 请求头
        :return: 响应数据
        """
        method = method.upper()
        if method == 'GET':
            response = requests.get(url=url, params=params, headers=header)
        elif method == 'POST':
            response = requests.post(url=url, data=data, json=json, headers=header)
        else:
            return 'REQUEST METHOD ERROR'
        if response is not None:
            return {
                'status_code': response.status_code,  # 响应状态码
                'data': response.json(),  # 响应主体
                'headers': response.headers,  # 响应头
                'time': response.elapsed.microseconds / 1000  # 响应时间
            }
        return response


if __name__ == '__main__':
    print(Request().send(url='http://www.baidu.com', method='get'))
